import os
import tkinter
import tkinter.simpledialog
import tkinter.colorchooser
import tkinter.filedialog
#
from PIL import ImageGrab

root = tkinter.Tk()
root.title('My Paint----by Dong Fuguo')
root['width']=800
root['height']=600

#
canDraw=tkinter.IntVar(value=0)

#
what=tkinter.IntVar(value=1)

#
X=tkinter.IntVar(value=0)
Y=tkinter.IntVar(value=0)

#
foreColor='#000000'
backColor='#FFFFFF'

#
image=tkinter.PhotoImage()
canv=tkinter.Canvas(root,
                      bg='white',
                      width=800,height=600)
canvas.create_image(800,600,image=image)

#
def onLeftButtonDown(event):
    canDraw.set(1)
    X.set(event.x)
    Y.set(event.y)
    if what.get()==4:
        canvas.creat_text(event.x,event.y,text=text)
canvas.bind('<Button-1>',onLeftButtonDown)

#
lastDraw=0
#
def onLeftButtonMove(event):
    global lastDraw
    if canDraw.get()==0:
        return
    if what.get()==1:
        #
        canvas.create_line(X.get(),
                             Y.get(),
                             event.x,
                             event.y,
                             fill=foreColor)
        X.set(event.x)
        Y.set(event.y)
    elif what.get()==2:
         #
         try:
             canvas.delete(lastDraw)
         except:
             pass
         lastDraw=canvas.create_line(X.get(),
                                        Y.get(),
                                        event.x,
                                        event.y,
                                        fill=foreColor)
    elif what.get()==3:
        #
        try:
            canvas.delete(lastDraw)
        except Exception as e:
            pass
        lastDraw=canvas.create_rectangle(X.get(),
                                          Y.get(),
                                          event.x,
                                          event.y,
                                          fill=foreColor,
                                          outline=foreColor)
    elif what.get()==5:
        #
        canvas.create_rectangle(event.x-5,
                                event.y-5,
                                event.x+5,
                                event.y+5,
                                outline=foreColor,
                                fill=backColor)
canvas.bind('<B1-Motion>',onLeftButtonMove)

#
def onLeftButtonUp(event):
    if what.get()==2:
       #
       canvas.create_line(X.get(),
                          Y.get(),
                          event.x,
                          event.y,
                          fill=foreColor)
    elif what.get()==3:
        #
        canvas.create_rectangle(X.get(),
                                Y.get(),
                                event.x,
                                event.y,
                                fill=foreColor,
                                outline=foreColor)
    canDraw.set(0)
    global lastDraw
    #
    lastDraw=0
canvas.bind('<ButtonRelease-1>',onLeftButtonUp)

#
menu=tkinter.Menu(root,tearoff=0)
#
def Open():
    filename=tkinter.filedialog.askopenfilename(title='Open Image',
                          filetypes=[('imagine','*.jpg *.png *.gif')])
    if filename:
       global image
       imagine=t5kinter.PhotoImage(file=filename)
       canvas.create_image(80,80,image=imagine)
menu.add_command(label='Open',command=Open)

def Save():
    #
    left_int(root.winfo_rootx())
    top_int(root.winfo_rooty())
    width=root.winfo_width()